home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 6984 / 6984.xpi / chrome / lazarus.jar / content / event.js < prev    next >
Text File  |  2009-11-24  |  9KB  |  277 lines

  1.  
  2. /**
  3. * === Custom events ===
  4. */
  5. Lazarus.Event = {};
  6.  
  7. //hash table of active handlers
  8. Lazarus.Event.handlers = {};
  9.  
  10. /* == list of firefox events ==
  11.  
  12. window-load          :Window has loaded, gBrowser has been initalized
  13. application-startup  :first window has loaded.
  14. application-shutdown :last window has closed
  15. window-unload        :Window is unloading
  16.  
  17. lazarus-installed   :Lazarus has been installed for the first time.
  18. lazarus-updated     :Extension is updating to a newer version.
  19.  
  20.  
  21. extension-uninstall(ext) :Extension has been uninstalled, and browser is closing.
  22. extension-uninstall-request(ext) :User has asked to have extension uninstall at new browser start.
  23. extension-uninstall-cancel(ext)  :User has cancelled previous uninstall request.
  24.  
  25. */
  26.  
  27.  
  28. /**
  29. * Add an event to the event queue
  30. * create a new queue if one doesn't already exist
  31. */
  32. Lazarus.Event.add = function(id, func){
  33.     //keep the handlers case insensitive
  34.     id = id.toLowerCase();
  35.     
  36.     //do not allow the same event to be added more than once.
  37.     var handlers = Lazarus.Event.handlers[id] || [];
  38.     for (var i=0; i<handlers.length; i++){
  39.         if (handlers[i] === func){
  40.             //handler already exists
  41.             return false;
  42.         }
  43.     }
  44.     handlers.push(func);
  45.     Lazarus.Event.handlers[id] = handlers;
  46.     return true;
  47. }
  48.  
  49. /**
  50. * Remove an event from the event queue
  51. * Does nothing if the event doesn't exist in the queue
  52. */
  53. Lazarus.Event.remove = function(id, func){
  54.     id = id.toLowerCase();
  55.     var handlers = Lazarus.Event.handlers[id] || [];
  56.     var newHandlers = [];
  57.     for (var i=0; i<handlers.length; i++){
  58.         if (handlers[i] != func){
  59.             newHandlers.push(handlers[i]);
  60.         }
  61.     }
  62.     Lazarus.Event.handlers[id] = newHandlers;
  63. }
  64.  
  65. /**
  66. * Triggers an event
  67. * Passes additional argument object to each of the event handlers in turn
  68. * if any event handler returns FALSE, then event will halt (no more handlers will be called)
  69. */
  70. Lazarus.Event.fire = function(id, args){
  71.     if (args){
  72.         Lazarus.debug("fire event: "+ id, args);
  73.     }
  74.     else {
  75.         Lazarus.debug("fire event: "+ id);
  76.     }
  77.     id = id.toLowerCase();
  78.     var handlers = Lazarus.Event.handlers[id] || [];
  79.     for (var i=0; i<handlers.length; i++){
  80.         try {
  81.             if (handlers[i](args) === false){
  82.                 break;
  83.             }
  84.         }
  85.         catch(e){
  86.             Lazarus.error(e);        
  87.         }
  88.     }
  89. }
  90.  
  91.  
  92. /**
  93. * return the number of current firefox windows
  94. */
  95. Lazarus.Event.numOfBrowserWindows = function(){
  96. //Use the nsIWindowManager to get a list of browser windows
  97.     var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  98.            .getService(Components.interfaces.nsIWindowMediator);
  99.     var en = wm.getEnumerator("navigator:browser");
  100.     var c = 0;
  101.     while(en.hasMoreElements()) {
  102.         c++;
  103.         en.getNext();
  104.     }
  105.     return c;
  106. }
  107.  
  108.  
  109. /**
  110. * fire load and startup events
  111. */
  112. Lazarus.Event.onWinLoad = function(){
  113.     if (gBrowser && !Lazarus.Event.onWinLoad.fired){
  114.         Lazarus.Event.onWinLoad.fired = true;
  115.         window.removeEventListener("load", Lazarus.Event.onWinLoad, false);
  116.         
  117.         //check for updates/install events
  118.         var currBuild = Lazarus.getVersionStr();
  119.         var prevBuild = Lazarus.getPref("extensions.lazarus.version");
  120.         Lazarus.setPref("extensions.lazarus.version", currBuild);
  121.         
  122.         //KLUDGE: moving to using version string to look for updated versions
  123.         if (!prevBuild && Lazarus.getPref("extensions.lazarus.build", 0)){
  124.             Lazarus.killPref("extensions.lazarus.build", 0)
  125.             prevBuild = "0.0.0";
  126.         }
  127.         
  128.         if (!prevBuild){
  129.             Lazarus.Event.fire("lazarus-installed");
  130.         }
  131.         else if (Lazarus.versionCompare(currBuild, prevBuild) == 1){
  132.             Lazarus.Event.fire("lazarus-updated", prevBuild);
  133.         }
  134.         Lazarus.Event.fire("window-load");
  135.         
  136.         //if this is the first window, fire startup
  137.         if (Lazarus.Event.numOfBrowserWindows() == 1){
  138.             Lazarus.Event.fire("application-startup");
  139.         }
  140.         
  141.         //look for webprogress events
  142.         Lazarus.Event.progressListener.init();
  143.     }
  144. }
  145.  
  146.  
  147. /**
  148. * handle the onunload event.
  149. */
  150. Lazarus.Event.onWinUnload = function(evt){
  151.     //the onUnload event if fired for a whole bunch of stuff 
  152.     //including a couple of times at startup, so we need to make sure it's
  153.     //the XUL document that is unloading.
  154.     if (evt.target === document){
  155.         window.removeEventListener("unload", Lazarus.Event.onWinUnload, true);
  156.         Lazarus.Event.progressListener.cleanup();
  157.         Lazarus.Event.fire("window-unload");
  158.     }
  159. }
  160.  
  161. /**
  162. * check for uninstall events
  163. */
  164. //ref: http://xulsolutions.blogspot.com/2006/07/creating-uninstall-script-for.html
  165. Lazarus.Event.nsIObserverService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  166.  
  167. Lazarus.Event.observer = {
  168.  
  169.     //dictionary of extensions waiting to be uninstalled
  170.     extensionsToBeUninstalled : {},
  171.     
  172.     //array of events to observe
  173.     topics : {
  174.         //final application window is closing 
  175.         "quit-application-granted" : function(subject, data){
  176.         
  177.             Lazarus.Event.fire("application-shutdown");
  178.             
  179.             for (var extId in Lazarus.Event.observer.extensionsToBeUninstalled){
  180.                 if (Lazarus.Event.observer.extensionsToBeUninstalled[extId]){
  181.                     Lazarus.Event.fire("extension-uninstall", extId);
  182.                 }
  183.             }            
  184.         },
  185.         
  186.         //extension manager actions (install/uninstall/uninstallrequest)
  187.         "em-action-requested" : function(subject, data){
  188.         
  189.             //convert subject into something we can access
  190.             subject.QueryInterface(Components.interfaces.nsIUpdateItem);
  191.             switch (data){
  192.                 case "item-uninstalled":
  193.                     Lazarus.Event.fire("extension-uninstall-request", subject);
  194.                     Lazarus.Event.observer.extensionsToBeUninstalled[subject.id] = true;
  195.                     break;
  196.                 
  197.                 case "item-cancel-action":
  198.                     Lazarus.Event.fire("extension-uninstall-cancel", subject);
  199.                     Lazarus.Event.observer.extensionsToBeUninstalled[subject.id] = false;
  200.                     break;  
  201.  
  202.                 default:
  203.                     //other extension manager event
  204.             }
  205.         }      
  206.     },
  207.     
  208.     //handle observe event
  209.     observe : function(subject, topic, data){
  210.         this.topics[topic](subject, data);
  211.     },
  212.     
  213.     //start observing
  214.     register : function() {
  215.         for (topic in this.topics){
  216.             Lazarus.Event.nsIObserverService.addObserver(this, topic, false);
  217.         }
  218.     },
  219.     
  220.     //stop observing
  221.     unregister : function() {
  222.         for (topic in this.topics){
  223.             Lazarus.Event.nsIObserverService.removeObserver(this, topic);
  224.         }
  225.     }
  226. }
  227.  
  228.  
  229. /**
  230. * nsIProgressListener
  231. */
  232. Lazarus.Event.progressListener = {
  233.     QueryInterface: function(aIID){
  234.         if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  235.             aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  236.             aIID.equals(Components.interfaces.nsISupports)){
  237.             return this;
  238.         }
  239.         else {
  240.             throw Components.results.NS_NOINTERFACE;
  241.         }
  242.     },
  243.  
  244.     onLocationChange: function(aProgress, aRequest, aURI){
  245.         Lazarus.Event.fire("location-change", aURI);
  246.     },
  247.     //unused
  248.     onStateChange: function(){},
  249.     onProgressChange: function(){},
  250.     onStatusChange: function(){},
  251.     onSecurityChange: function(){},
  252.     onLinkIconAvailable: function(){},
  253.     
  254.     init: function(){
  255.         gBrowser.addProgressListener(Lazarus.Event.progressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
  256.     },
  257.     
  258.     cleanup: function(){
  259.         gBrowser.removeProgressListener(Lazarus.Event.progressListener);
  260.     }
  261. }
  262.  
  263. /**
  264. * add event handlers to this window
  265. */
  266. Lazarus.Event.init = function(){
  267.     window.addEventListener("load", Lazarus.Event.onWinLoad, false);
  268.     window.addEventListener("unload", Lazarus.Event.onWinUnload, true);
  269.     Lazarus.Event.observer.register();
  270.         
  271.         //cleanup if we're closing
  272.         window.addEventListener("unload", function(){
  273.             Lazarus.Event.observer.unregister();
  274.         }, false);
  275. }
  276.  
  277.